package com.ccx.library.Interceptor.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.ccx.library.module.user.entity.User;

public class StatusConverter implements Converter<Integer> {

    private final static Integer STATUS_START = 0;
    private final static Integer STATUS_STOP = 1;

    @Override
    public Class<?> supportJavaTypeKey() {
        return Converter.super.supportJavaTypeKey();
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return Converter.super.supportExcelTypeKey();
    }

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return Converter.super.convertToJavaData(cellData, contentProperty, globalConfiguration);
    }

    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) throws Exception {
        if (context.getReadCellData().getStringValue().equals("正常") || context.getReadCellData().getStringValue().equals("回收")){
            return 0;
        }
        if(context.getReadCellData().getStringValue().equals("禁用") || context.getReadCellData().getStringValue().equals("未回收")){
            return 1;
        }
        return Converter.super.convertToJavaData(context);
    }

    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if(contentProperty.getField().getName().equals("status")) {
            return new WriteCellData(value.equals(STATUS_START) ? "正常" : "禁用");
        }
        if (contentProperty.getField().getName().equals("flag")){
            return new WriteCellData(value.equals(STATUS_START) ? "回收" : "未回收" );
        }
        return Converter.super.convertToExcelData(value,contentProperty,globalConfiguration);
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) throws Exception {
        return Converter.super.convertToExcelData(context);
    }
}
